home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / cview.jar / content / cview / cview-utils.js < prev    next >
Encoding:
JavaScript  |  2005-11-25  |  11.9 KB  |  529 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is mozilla.org code.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Robert Ginda, rginda@netscape.com, original author
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /*
  41.  * This file is (at the time of this writing) an exact copy of the chatzilla
  42.  * utility functions.  It should actually be globally accessible (not just
  43.  * part of chatzilla.)  This hasn't happened yet, so it's copied here to
  44.  * avoid a dependancy on chatzilla
  45.  */
  46.  
  47. var dumpln;
  48. if (typeof document == "undefined") /* in xpcshell */
  49.     dumpln = print;
  50. else
  51.     if (typeof dump == "function")
  52.         dumpln = function (str) {dump (str + "\n");}
  53.     else if (jsenv.HAS_RHINO)
  54.         dumpln = function (str) {var out = java.lang.System.out;
  55.                                  out.println(str); out.flush(); }
  56.     else
  57.         dumpln = function () {} /* no suitable function */
  58.  
  59. var dd;
  60. if (DEBUG)
  61.     dd = dumpln;
  62. else
  63.     dd = function (){};
  64.  
  65. var jsenv = new Object();
  66. jsenv.HAS_SECURITYMANAGER = ((typeof netscape == "object") &&
  67.                              (typeof netscape.security == "object"));
  68. jsenv.HAS_XPCOM = ((typeof Components == "function") &&
  69.                    (typeof Components.classes == "function"));
  70. jsenv.HAS_JAVA = (typeof java == "object");
  71. jsenv.HAS_RHINO = (typeof defineClass == "function");
  72. jsenv.HAS_DOCUMENT = (typeof document == "object");
  73.  
  74. function dumpObject (o, pfx, sep)
  75. {
  76.     var p;
  77.     var s = "";
  78.  
  79.     sep = (typeof sep == "undefined") ? " = " : sep;
  80.     pfx = (typeof pfx == "undefined") ? "" : pfx;    
  81.  
  82.     for (p in o)
  83.     {
  84.         if (typeof (o[p]) != "function")
  85.             s += pfx + p + sep + o[p] + "\n";
  86.         else
  87.             s += pfx + p + sep + "function\n";
  88.     }
  89.  
  90.     return s;
  91.  
  92. }
  93.  
  94. /* Dumps an object in tree format, recurse specifiec the the number of objects
  95.  * to recurse, compress is a boolean that can uncompress (true) the output
  96.  * format, and level is the number of levels to intitialy indent (only useful
  97.  * internally.)  A sample dumpObjectTree (o, 1) is shown below.
  98.  *
  99.  * + parent (object)
  100.  * + users (object)
  101.  * | + jsbot (object)
  102.  * | + mrjs (object)
  103.  * | + nakkezzzz (object)
  104.  * | *
  105.  * + bans (object)
  106.  * | *
  107.  * + topic (string) 'ircclient.js:59: nothing is not defined'
  108.  * + getUsersLength (function) 9 lines
  109.  * *
  110.  */
  111. function dumpObjectTree (o, recurse, compress, level)
  112. {
  113.     var s = "";
  114.     var pfx = "";
  115.  
  116.     if (typeof recurse == "undefined")
  117.         recurse = 0;
  118.     if (typeof level == "undefined")
  119.         level = 0;
  120.     if (typeof compress == "undefined")
  121.         compress = true;
  122.     
  123.     for (var i = 0; i < level; i++)
  124.         pfx += (compress) ? "| " : "|  ";
  125.  
  126.     var tee = (compress) ? "+ " : "+- ";
  127.  
  128.     for (i in o)
  129.     {
  130.         
  131.         var t = typeof o[i];
  132.         switch (t)
  133.         {
  134.             case "function":
  135.                 var sfunc = o[i].toString().split("\n");
  136.                 if (sfunc[2] == "    [native code]")
  137.                     sfunc = "[native code]";
  138.                 else
  139.                     sfunc = sfunc.length + " lines";
  140.                 s += pfx + tee + i + " (function) " + sfunc + "\n";
  141.                 break;
  142.  
  143.             case "object":
  144.                 s += pfx + tee + i + " (object)\n";
  145.                 if (!compress)
  146.                     s += pfx + "|\n";
  147.                 if ((i != "parent") && (recurse))
  148.                     s += dumpObjectTree (o[i], recurse - 1,
  149.                                          compress, level + 1);
  150.                 break;
  151.  
  152.             case "string":
  153.                 if (o[i].length > 200)
  154.                     s += pfx + tee + i + " (" + t + ") " + 
  155.                         o[i].length + " chars\n";
  156.                 else
  157.                     s += pfx + tee + i + " (" + t + ") '" + o[i] + "'\n";
  158.                 break;
  159.  
  160.             default:
  161.                 s += pfx + tee + i + " (" + t + ") " + o[i] + "\n";
  162.                 
  163.         }
  164.  
  165.         if (!compress)
  166.             s += pfx + "|\n";
  167.  
  168.     }
  169.  
  170.     s += pfx + "*\n";
  171.     
  172.     return s;
  173.     
  174. }
  175.  
  176. /*
  177.  * Clones an existing object (Only the enumerable properties
  178.  * of course.) use as a function..
  179.  * var c = Clone (obj);
  180.  * or a constructor...
  181.  * var c = new Clone (obj);
  182.  */
  183. function Clone (obj)
  184. {
  185.     robj = new Object();
  186.  
  187.     for (var p in obj)
  188.         robj[p] = obj[p];
  189.  
  190.     return robj;
  191.     
  192. }
  193.  
  194. /*
  195.  * matches a real object against one or more pattern objects.
  196.  * if you pass an array of pattern objects, |negate| controls whether to check
  197.  * if the object matches ANY of the patterns, or NONE of the patterns.
  198.  */
  199. function matchObject (o, pattern, negate)
  200. {
  201.     negate = Boolean(negate);
  202.     
  203.     function _match (o, pattern)
  204.     {
  205.         if (pattern instanceof Function)
  206.             return pattern(o);
  207.         
  208.         for (p in pattern)
  209.         {
  210.             var val;
  211.                 /* nice to have, but slow as molases, allows you to match
  212.                  * properties of objects with obj$prop: "foo" syntax      */
  213.                 /*
  214.                   if (p[0] == "$")
  215.                   val = eval ("o." + 
  216.                   p.substr(1,p.length).replace (/\$/g, "."));
  217.                   else
  218.                 */
  219.             val = o[p];
  220.             
  221.             if (pattern[p] instanceof Function)
  222.             {
  223.                 if (!pattern[p](val))
  224.                     return false;
  225.             }
  226.             else
  227.             {
  228.                 var ary = (new String(val)).match(pattern[p]);
  229.                 if (ary == null)
  230.                     return false;
  231.                 else
  232.                     o.matchresult = ary;
  233.             }
  234.         }
  235.  
  236.         return true;
  237.  
  238.     }
  239.  
  240.     if (!(pattern instanceof Array))
  241.         return Boolean (negate ^ _match(o, pattern));
  242.             
  243.     for (var i in pattern)
  244.         if (_match (o, pattern[i]))
  245.             return !negate;
  246.  
  247.     return negate;
  248.     
  249. }
  250.  
  251. function matchEntry (partialName, list)
  252. {
  253.     
  254.     if ((typeof partialName == "undefined") ||
  255.         (String(partialName) == ""))
  256.         return list;
  257.  
  258.     var ary = new Array();
  259.  
  260.     for (var i in list)
  261.     {
  262.         if (list[i].indexOf(partialName) == 0)
  263.             ary.push (list[i]);
  264.     }
  265.  
  266.     return ary;
  267.     
  268. }
  269.  
  270. function getCommonPfx (list)
  271. {
  272.     var pfx = list[0];
  273.     var l = list.length;
  274.     
  275.     for (var i = 1; i < l; i++)
  276.     {
  277.         for (var c = 0; c < pfx.length; c++)
  278.             if (pfx[c] != list[i][c])
  279.                 pfx = pfx.substr (0, c);
  280.     }
  281.  
  282.     return pfx;
  283.  
  284. }
  285.  
  286. function renameProperty (obj, oldname, newname)
  287. {
  288.  
  289.     if (oldname == newname)
  290.         return;
  291.     
  292.     obj[newname] = obj[oldname];
  293.     delete obj[oldname];
  294.     
  295. }
  296.  
  297. function newObject(contractID, iface)
  298. {
  299.     if (!jsenv.HAS_XPCOM)
  300.         return null;
  301.  
  302.     var obj = Components.classes[contractID].createInstance();
  303.     var rv;
  304.  
  305.     switch (typeof iface)
  306.     {
  307.         case "string":
  308.             rv = obj.QueryInterface(Components.interfaces[iface]);
  309.             break;
  310.  
  311.         case "object":
  312.             rv = obj.QueryInterface[iface];
  313.             break;
  314.  
  315.         default:
  316.             rv = null;
  317.             break;
  318.     }
  319.  
  320.     return rv;
  321.     
  322. }
  323.  
  324. function getPriv (priv)
  325. {
  326.     if (!jsenv.HAS_SECURITYMANAGER)
  327.         return true;
  328.  
  329.     var rv = true;
  330.  
  331.     try
  332.     {
  333.         netscape.security.PrivilegeManager.enablePrivilege(priv);
  334.     }
  335.     catch (e)
  336.     {
  337.         dd ("getPriv: unable to get privlege '" + priv + "': " + e);
  338.         rv = false;
  339.     }
  340.     
  341.     return rv;
  342.     
  343. }
  344.  
  345. function keys (o)
  346. {
  347.     var rv = new Array();
  348.     
  349.     for (var p in o)
  350.         rv.push(p);
  351.  
  352.     return rv;
  353.     
  354. }
  355.  
  356. function stringTrim (s)
  357. {
  358.     if (!s)
  359.         return "";
  360.     s = s.replace (/^\s+/, "");
  361.     return s.replace (/\s+$/, "");
  362.     
  363. }
  364.  
  365.  
  366. function arrayInsertAt (ary, i, o)
  367. {
  368.  
  369.     ary.splice (i, 0, o);
  370.  
  371.     /* doh, forgot about that 'splice' thing
  372.     if (ary.length < i)
  373.     {
  374.         this[i] = o;
  375.         return;
  376.     }
  377.  
  378.     for (var j = ary.length; j > i; j--)
  379.         ary[j] = ary[j - 1];
  380.  
  381.     ary[i] = o;
  382.     */
  383. }
  384.  
  385. function arrayRemoveAt (ary, i)
  386. {
  387.  
  388.     ary.splice (i, 1);
  389.  
  390.     /* doh, forgot about that 'splice' thing
  391.     if (ary.length < i)
  392.         return false;
  393.  
  394.     for (var j = i; j < ary.length; j++)
  395.         ary[j] = ary[j + 1];
  396.  
  397.     ary.length--;
  398.     */
  399.  
  400. }
  401.  
  402. /* length should be an even number >= 6 */
  403. function abbreviateWord (str, length)
  404. {
  405.     if (str.length <= length || length < 6)
  406.         return str;
  407.  
  408.     var left = str.substr (0, (length / 2) - 1);
  409.     var right = str.substr (str.length - (length / 2) + 1);
  410.  
  411.     return left + "..." + right;
  412. }
  413.  
  414. function getRandomElement (ary)
  415. {
  416.     var i = parseInt (Math.random() * ary.length)
  417.     if (i == ary.length) i = 0;
  418.  
  419.     return ary[i];
  420.  
  421. }
  422.  
  423. function roundTo (num, prec)
  424. {
  425.  
  426.     return parseInt (( Math.round(num) * Math.pow (10, prec))) /
  427.         Math.pow (10, prec);   
  428.  
  429. }
  430.  
  431. function randomRange (min, max)
  432. {
  433.  
  434.     if (typeof min == "undefined")
  435.         min = 0;
  436.  
  437.     if (typeof max == "undefined")
  438.         max = 1;
  439.  
  440.     var rv = (parseInt(Math.round((Math.random() * (max - min)) + min )));
  441.     
  442.     return rv;
  443.  
  444. }
  445.  
  446. function getStackTrace ()
  447. {
  448.  
  449.     if (!jsenv.HAS_XPCOM)
  450.         return "No stack trace available.";
  451.  
  452.     var frame = Components.stack.caller;
  453.     var str = "<top>";
  454.  
  455.     while (frame)
  456.     {
  457.         var name = frame.functionName ? frame.functionName : "[anonymous]";
  458.         str += "\n" + name + "@" + frame.lineNumber;
  459.         frame = frame.caller;
  460.     }
  461.  
  462.     return str;
  463.     
  464. }
  465.  
  466. function getInterfaces (cls)
  467. {
  468.     if (!jsenv.HAS_XPCOM)
  469.         return null;
  470.  
  471.     var rv = new Object();
  472.     var e;
  473.  
  474.     for (var i in Components.interfaces)
  475.     {
  476.         try
  477.         {
  478.             var ifc = Components.interfaces[i];
  479.             cls.QueryInterface(ifc);
  480.             rv[i] = ifc;
  481.         }
  482.         catch (e)
  483.         {
  484.             /* nada */
  485.         }
  486.     }
  487.  
  488.     return rv;
  489.     
  490. }
  491.  
  492. /**
  493.  * Calls a named function for each element in an array, sending
  494.  * the same parameter each call.
  495.  *
  496.  * @param ary           an array of objects
  497.  * @param func_name     string name of function to call.
  498.  * @param data          data object to pass to each object.
  499.  */      
  500. function mapObjFunc(ary, func_name, data)
  501. {
  502.     /* 
  503.      * WARNING: Caller assumes resonsibility to verify ary
  504.      * and func_name
  505.      */
  506.  
  507.     for (var i in ary)
  508.         ary[i][func_name](data);
  509. }
  510.  
  511. /**
  512.  * Passes each element of an array to a given function object.
  513.  *
  514.  * @param func  a function object.
  515.  * @param ary   an array of values.
  516.  */
  517. function map(func, ary) {
  518.  
  519.     /*
  520.      * WARNING: Caller assumnes responsibility to verify
  521.      * func and ary.
  522.      */
  523.  
  524.     for (var i in ary)
  525.         func(ary[i]);
  526.  
  527. }
  528.  
  529.